#include"contact.h"

void InitContact(Contact* pcnt)
{
	memset(pcnt->Cont, 0, sizeof(PersonInfo) * MAX_CONTACT_SIZE);
	pcnt->capacity = MAX_CONTACT_SIZE;
	pcnt->size = 0;
}
bool IsFull(Contact* pcnt)
{
	return pcnt->size >= pcnt->capacity;
}

void AddContact(Contact* pcnt)
{
	//检查容量
	if (IsFull(pcnt))
	{
		printf("通讯录已满!");
		return;
	}
	printf("姓名:");
	scanf("%s", pcnt->Cont[pcnt->size].name);
	printf("性别:");
	scanf("%s", pcnt->Cont[pcnt->size].sex);
	printf("年龄:");
	scanf("%d", &pcnt->Cont[pcnt->size].age);
	printf("电话:");
	scanf("%s", pcnt->Cont[pcnt->size].tel);
	printf("地址:");
	scanf("%s", pcnt->Cont[pcnt->size].add);
	pcnt->size++;
	printf("增加人员信息成功!\n");
}
int FindByName(Contact* pcnt,char name[])
{
	for (int i = 0; i < pcnt->size; ++i)
	{
		if (strcmp(name, pcnt->Cont[i].name) == 0)
		{
			return i;
		}
	}
	return -1;
}

void FindContact(Contact* pcnt)
{
	if (IsEmpty(pcnt))
	{
		printf("通讯录为空!\n");
		return;
	}
	char name[MAX_NAME_SIZE];
	printf("请输入姓名:");
	scanf("%s", name);

	int index = FindByName(pcnt,name);
	if (index == -1)
	{
		printf("此人不存在!\n");
		return;
	}
	printf("%-15s%-15s%-15s%-15s%-15s", "姓名", "性别", "年龄", "电话", "地址");
	printf("\n");
	printf("%-13s%-13s%-13d%-13s%-13s\n", pcnt->Cont[index].name, pcnt->Cont[index].sex, pcnt->Cont[index].age, pcnt->Cont[index].tel, pcnt->Cont[index].add);
}
bool IsEmpty(Contact* pcnt)
{
	return pcnt->size == 0;
}
void ShowContact(Contact* pcnt)
{
	printf("%-15s%-15s%-15s%-15s%-15s","姓名","性别","年龄","电话","地址");
	printf("\n");
	for (int i = 0; i < pcnt->size; ++i)
	{
		printf("%-13s%-13s%-13d%-13s%-13s\n", pcnt->Cont[i].name, pcnt->Cont[i].sex, pcnt->Cont[i].age, pcnt->Cont[i].tel, pcnt->Cont[i].add);
	}
}
void ModifyContact(Contact* pcnt)
{
	if (IsEmpty(pcnt))
	{
		printf("通讯录为空!\n");
		return;
	}
	printf("请输入姓名:");
	char name[MAX_NAME_SIZE];
	scanf("%s", name);
	int index = FindByName(pcnt,name);
	if (index == -1)
	{
		printf("此人不存在!\n");
		return;
	}
	printf("你要修改什么信息(1-姓名 2-性别 3-年龄 4-电话 5-住址)\n");
	int sel;
	scanf("%d", &sel);
	switch (sel)
	{
	case 1:
		printf("请输入要修改的姓名:");
		scanf("%s", pcnt->Cont[index].name);
		break;
	case 2:
		printf("请输入要修改的性别:");
		scanf("%s", pcnt->Cont[index].sex);
		break;
	case 3:
		printf("请输入要修改的年龄:");
		scanf("%d",&pcnt->Cont[index].age);
		break;
	case 4:
		printf("请输入要修改的电话:");
		scanf("%s", pcnt->Cont[index].tel);
		break;
	case 5:
		printf("请输入要修改的地址:");
		scanf("%s", pcnt->Cont[index].add);
		break;
	}
}
void DelContact(Contact* pcnt)
{
	printf("请输入要删除的名字:");
	char name[MAX_NAME_SIZE];
	scanf("%s", name);
	int index = FindByName(pcnt, name);
	if (index == -1)
	{
		printf("此人不存在!\n");
		return;
	}
	for (int i = index; i < pcnt->size - 1; ++i)
	{
		pcnt->Cont[i] = pcnt->Cont[i + 1];
	}
	pcnt->size--;
	printf("删除完成!\n");
}
void ClearContact(Contact* pcnt)
{
	printf("是否确定 <Y/N> ?:\n");
	getchar();
	char ch = getchar();
	if (ch == 'Y' || ch == 'y')
	{
		pcnt->size = 0;
		printf("清除成功!\n");
		return;
	}
	else if(ch == 'N' || ch == 'n')
	{
		printf("清楚取消!\n");
		return;
	}
	printf("输入错误!\n");
}
void SortContact(Contact* pcnt)
{
	for (int i = 0; i < pcnt->size - 1; ++i)
	{
		for (int j = 0; j < pcnt->size - i - 1; ++j)
		{
			if (strcmp(pcnt->Cont[j].name, pcnt->Cont[j + 1].name) > 0)
			{
				PersonInfo tmp = pcnt->Cont[j];
				pcnt->Cont[j] = pcnt->Cont[j + 1];
				pcnt->Cont[j + 1] = tmp;
			}
		}
	}
	printf("排序成功!\n");
}